#!/bin/bash
#
# Script to generate unit test coverage report, requires lcov:
#
# http://ltp.sourceforge.net/coverage/lcov.php
#

# Tests with coverage enabled:
TESTS="\
 test-common \
 test-util \
 test-idmap \
 test-simutil \
 test-stkutil \
 test-sms \
 test-cdmasms \
 test-sms-root \
 test-caif \
 test-dbus-queue \
 test-dbus-access \
 test-gprs-filter \
 test-provision \
 test-config \
 test-watch \
 test-ril_util \
 test-ril_config \
 test-ril_ecclist \
 test-ril-transport \
 test-ril_vendor \
 test-sms-filter \
 test-voicecall-filter \
 test-sailfish_access \
 test-sailfish_cell_info \
 test-sailfish_cell_info_dbus \
 test-sailfish_manager \
 test-sailfish_sim_info \
 test-sailfish_sim_info_dbus"

pushd `dirname $0` > /dev/null
TEST_DIR="$PWD"
pushd .. > /dev/null
BASE_DIR="$PWD"
popd > /dev/null
popd > /dev/null

FULL_COV="$TEST_DIR/full.gcov"
DRIVERS_COV="$TEST_DIR/drivers.gcov"
PLUGINS_COV="$TEST_DIR/plugins.gcov"
SRC_COV="$TEST_DIR/src.gcov"
OUT="$TEST_DIR/html"

# Clean everything up
find "$BASE_DIR" -name "*.gcda" -exec rm {} \;
rm -f "$FULL_COV" "$DRIVERS_COV" "$PLUGINS_COV" "$SRC_COV"
rm -fr "$OUT"

# Run the tests
for t in $TESTS ; do
    pushd "$TEST_DIR" > /dev/null
    "$TEST_DIR/$t"
    RC=$?
    popd > /dev/null
    [ $RC = 0 ] || exit 1
done

# LCOV 1.10 has branch coverage disabled per default
LCOV_OPT="--rc lcov_branch_coverage=1"
GENHTML_OPT="--branch-coverage"

lcov $LCOV_OPT -c -d "$BASE_DIR" -o "$FULL_COV" || exit 1
lcov $LCOV_OPT -e "$FULL_COV" "$BASE_DIR/drivers/*" -o "$DRIVERS_COV" || exit 1
lcov $LCOV_OPT -e "$FULL_COV" "$BASE_DIR/plugins/*" -o "$PLUGINS_COV" || exit 1
lcov $LCOV_OPT -e "$FULL_COV" "$BASE_DIR/src/*" -o "$SRC_COV" || exit 1
genhtml $GENHTML_OPT -t ofono "$DRIVERS_COV" "$PLUGINS_COV" "$SRC_COV" --output-directory "$OUT" || exit 1

echo Coverage report: $OUT/index.html
